/**
 * 添加引用
 * @file
 * @since 1.2.6.1
 */

/**
 * 添加引用
 * @command blockquote
 * @method execCommand
 * @param { String } cmd 命令字符串
 * @example
 * ```javascript
 * editor.execCommand( 'blockquote' );
 * ```
 */

/**
 * 添加引用
 * @command blockquote
 * @method execCommand
 * @param { String } cmd 命令字符串
 * @param { Object } attrs 节点属性
 * @example
 * ```javascript
 * editor.execCommand( 'blockquote',{
 *     style: "color: red;"
 * } );
 * ```
 */

UE.plugins["blockquote"] = function () {
    var me = this;

    function getObj(editor) {
        return domUtils.filterNodeList(
            editor.selection.getStartElementPath(),
            "blockquote"
        );
    }

    me.commands["blockquote"] = {
        execCommand: function (cmdName, attrs) {
            var range = this.selection.getRange(),
                obj = getObj(this),
                blockquote = dtd.blockquote,
                bookmark = range.createBookmark();

            if (obj) {
                var start = range.startContainer,
                    startBlock = domUtils.isBlockElm(start)
                        ? start
                        : domUtils.findParent(start, function (node) {
                            return domUtils.isBlockElm(node);
                        }),
                    end = range.endContainer,
                    endBlock = domUtils.isBlockElm(end)
                        ? end
                        : domUtils.findParent(end, function (node) {
                            return domUtils.isBlockElm(node);
                        });

                //处理一下li
                startBlock =
                    domUtils.findParentByTagName(startBlock, "li", true) || startBlock;
                endBlock =
                    domUtils.findParentByTagName(endBlock, "li", true) || endBlock;

                if (
                    startBlock.tagName == "LI" ||
                    startBlock.tagName == "TD" ||
                    startBlock === obj ||
                    domUtils.isBody(startBlock)
                ) {
                    domUtils.remove(obj, true);
                } else {
                    domUtils.breakParent(startBlock, obj);
                }

                if (startBlock !== endBlock) {
                    obj = domUtils.findParentByTagName(endBlock, "blockquote");
                    if (obj) {
                        if (
                            endBlock.tagName == "LI" ||
                            endBlock.tagName == "TD" ||
                            domUtils.isBody(endBlock)
                        ) {
                            obj.parentNode && domUtils.remove(obj, true);
                        } else {
                            domUtils.breakParent(endBlock, obj);
                        }
                    }
                }

                var blockquotes = domUtils.getElementsByTagName(
                    this.document,
                    "blockquote"
                );
                for (var i = 0, bi; (bi = blockquotes[i++]);) {
                    if (!bi.childNodes.length) {
                        domUtils.remove(bi);
                    } else if (
                        domUtils.getPosition(bi, startBlock) &
                        domUtils.POSITION_FOLLOWING &&
                        domUtils.getPosition(bi, endBlock) & domUtils.POSITION_PRECEDING
                    ) {
                        domUtils.remove(bi, true);
                    }
                }
            } else {
                var tmpRange = range.cloneRange(),
                    node = tmpRange.startContainer.nodeType == 1
                        ? tmpRange.startContainer
                        : tmpRange.startContainer.parentNode,
                    preNode = node,
                    doEnd = 1;

                //调整开始
                while (1) {
                    if (domUtils.isBody(node)) {
                        if (preNode !== node) {
                            if (range.collapsed) {
                                tmpRange.selectNode(preNode);
                                doEnd = 0;
                            } else {
                                tmpRange.setStartBefore(preNode);
                            }
                        } else {
                            tmpRange.setStart(node, 0);
                        }

                        break;
                    }
                    if (!blockquote[node.tagName]) {
                        if (range.collapsed) {
                            tmpRange.selectNode(preNode);
                        } else {
                            tmpRange.setStartBefore(preNode);
                        }
                        break;
                    }

                    preNode = node;
                    node = node.parentNode;
                }

                //调整结束
                if (doEnd) {
                    preNode = node = node = tmpRange.endContainer.nodeType == 1
                        ? tmpRange.endContainer
                        : tmpRange.endContainer.parentNode;
                    while (1) {
                        if (domUtils.isBody(node)) {
                            if (preNode !== node) {
                                tmpRange.setEndAfter(preNode);
                            } else {
                                tmpRange.setEnd(node, node.childNodes.length);
                            }

                            break;
                        }
                        if (!blockquote[node.tagName]) {
                            tmpRange.setEndAfter(preNode);
                            break;
                        }

                        preNode = node;
                        node = node.parentNode;
                    }
                }

                node = range.document.createElement("blockquote");
                domUtils.setAttributes(node, attrs);
                node.appendChild(tmpRange.extractContents());
                tmpRange.insertNode(node);
                //去除重复的
                var childs = domUtils.getElementsByTagName(node, "blockquote");
                for (var i = 0, ci; (ci = childs[i++]);) {
                    if (ci.parentNode) {
                        domUtils.remove(ci, true);
                    }
                }
            }
            range.moveToBookmark(bookmark).select();
        },
        queryCommandState: function () {
            return getObj(this) ? 1 : 0;
        }
    };
};
